fpath = '1-0.bin'; %'balloons.png'; 

VISUALIZE = 0;  %0 : no visualization validate output file, 1: visualize matlab output

nr = 256; %128; %256
nc = 256; %128; %256;

inputvideo = 'video.avi';
fileformat = 0; 
% 0 : image (png, bmp)
% 1 : binary (.bin)
% 2 : video (.avi)

Nc = 3; %1; % num of center channels

% bin_dat_bar = 1;     %0 : dat file (ascii), 1 : binary file

output_nr = 16; %8; %16; % size of output saliency
output_nc = 16; %8; %16;
%% for comparing hardware result
result_file = 'saliency_IOMF_1.dat'; %1-o_BAK.bin'; %'sal_IOM_128_out.bin'; %'motion_result.dat';  %'../saliency_ml605/orientation_channel_result.dat'; 
error_threshold = 1e-3;
result_flag = 1;

NUM_FRAMES = 2; %100; %201;
FRAME_SWAP = 1;
bin_dat_bar = 0;     %0 : dat file (ascii), 1 : binary file
% res_fname_0 = '1-o_BAK.bin';
% res_fname_1 = '2-o_BAK.bin';
if (bin_dat_bar == 1)
    outfilepath= 'R:\fourier\MSPark\Saliency API\NeoEnvironment\Applications\streaming_loopback_v1_00_a\Data256\'; %'R:\kesturvy\NEO_VISION\DNDUALV6_PCIE4\demo_oct2010\Data256\'; %'..\..\..\demo_dec2010\Data\';
else
    outfilepath = 'results\saliency_retina\'; %'results\beta_CFIOM_6cs_256\';
end
logfile = 'output.log';

if(VISUALIZE == 1)  %if visualization enabled
    scrsz= get(0,'ScreenSize');
%     h = figure('position',[200 scrsz(4)/3 scrsz(3)/2 scrsz(4)/3]);
    h = figure('position',[10 scrsz(4)/3 scrsz(3) scrsz(4)/3]);
       
    p0 = subplot(1,3,1);
    axis equal
    %%set(gca,'nextplot', 'replacechildren');
    p1 = subplot(1,3,2);
    axis equal    
    p2 = subplot(1,3,3);
    axis equal    

%     p0 = subplot(1,2,1);
%     axis equal
%     p2 = subplot(1,2,2);
%     axis equal       

end

logfile_id = fopen(logfile,'w+');   %open the log file
qout = quantizer('fixed', [24,20]);  %output data rep

  mov = aviread(inputvideo) ; 
  
  frame_swap = zeros(nr,nc,3);
  
 out_mov(1:NUM_FRAMES) = struct('cdata', [],'colormap', []);
                        
   for i = 1:NUM_FRAMES  %size(mov,2) %will go to number of frames
 
    if(i == 1)
        im_prev = zeros(size(mov(1,i).cdata));
    else
        im_prev = frame;
    end
    frame = mov(1,i).cdata;
   
    if(FRAME_SWAP == 1) 
        r = frame(:,:,1);
        r1 = reshape(r', 2, nr*nc/2);
        r2 = r1;
        r2(1,:) = r1(2,:);
        r2(2,:) = r1(1,:);
        r_swap = reshape(r2, nr, nc)';

        g = frame(:,:,2);
        r1 = reshape(g', 2, nr*nc/2);
        r2 = r1;
        r2(1,:) = r1(2,:);
        r2(2,:) = r1(1,:);
        g_swap = reshape(r2, nr, nc)';

        b = frame(:,:,3);
        r1 = reshape(b', 2, nr*nc/2);
        r2 = r1;
        r2(1,:) = r1(2,:);
        r2(2,:) = r1(1,:);
        b_swap = reshape(r2, nr, nc)';

        frame(:,:,1) = r_swap;
        frame(:,:,2) = g_swap;
        frame(:,:,3) = b_swap;
    end
    
%     im2 = imresize(im2, [nr,nc]);   % resize to desired img size
%     img = double(im2)./255 ;
%     [I R G B Y] = SalColorMap(img);   % Generate I, RG and BY channels 
%     
%     img = I; %abs(R - G);   
%     img3 = R-G;
%     img4 = B-Y; 
%     
%     %-----------------Int
%     img_q = quantize(q, img);
%     
    im_curr = frame;
    
    
    [Sal, iSal, cSal, oSal, mSal, fSal] = saliency_hw_sim(im_prev, im_curr, nr, nc, Nc, i);      

    

    if(bin_dat_bar == 1)    %binary file
        out_filename=strcat(outfilepath,num2str(i),'-','o_BAK','.','bin');     %check file name i-o-BAK.bin exixts
        check_file =0;
        while (check_file ~= 2)
            check_file= exist (out_filename, 'file');
        end
        file_id = fopen(out_filename,'r');
        Bdec=dec2hex(fread(file_id,inf,'uint32'));
        array_out= hex2num(qout,Bdec); 
        fclose(file_id);
        result_hw = reshape(array_out, output_nc, output_nr)';
    elseif(bin_dat_bar == 0)  %dat file
        out_filename=strcat(outfilepath,num2str(i),'-','o_BAK','.','dat');     %check file name i-o-BAK.bin exixts
        check_file =0;
        while (check_file ~= 2)
            check_file= exist (out_filename, 'file');
        end
        array_out = dat2array(out_filename, output_nr*output_nc, qout);          
        result_hw = reshape(array_out, output_nc, output_nr)';
    end

    if(VISUALIZE)
        axes(p0)
        imagesc(imresize(im_curr, [nr,nc]));
%         title (['Input Frame  ']);
        title (['Input Frame  ', num2str(i)]);

        axes(p1)
        imagesc(imresize(Sal, [nr,nc]));        %Visualize REF output
        title (['Ref Saliency  ', num2str(i)]);

        axes(p2)
        imagesc(imresize(result_hw,[nr,nc]));   %Visualize HARDWARE output
%         title (['FPGA Saliency  ']);
        title (['FPGA Saliency  ', num2str(i)]); 
        
        pause(0.01) %0.25
  %      out_mov(i)= getframe(h);
    end
    
    abs_error = abs((Sal) - (result_hw));
    max_error = max(max(abs_error));
    avg_error = mean(mean(abs_error));       

    error_flag = abs_error > error_threshold;
    num_error = sum(sum(error_flag));
    error_flag = error_flag * 1111;


    fprintf(logfile_id,'Frame %d - num error = %d, max error = %E, avg error = %E\n', i, num_error, max_error, avg_error);       
    
   end
   
%close the log file
 fclose(logfile_id);

 %% Make movie 
%  close(h);
 %movie2avi(out_mov, 'saliency_6cs_256_SX475.avi', 'compression', 'None');
 
        
